package com.kavadani.profiles;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.kavadani.app.PatrolCarApp;
import com.kavadani.dialogs.ErrorDlg;

public class EnterPassword {
	
	public static final void show(final Context context, final ProfileItem user, final DialogInterface.OnClickListener onClickListener) {
		LayoutInflater factory = LayoutInflater.from(context);
		final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
		
		final EditText usernameText = (EditText) textEntryView.findViewById(R.id.username_edit);
		usernameText.setText(user.getText());
		usernameText.setFocusable(false);
		/*
		if(app.username != null && app.password != null) {
			
			EditText text = (EditText) textEntryView.findViewById(R.id.username_edit);
			text.setText(app.username);
			text = (EditText) textEntryView.findViewById(R.id.password_edit);
			text.setText(app.password);
			}
			catch(Exception ex) {
				Log.e(TaxiManagerApp.TAG, "Login: set fields error: " + ex.getMessage());
			}
		}
		*/
		
		AlertDialog loginDialog = new AlertDialog.Builder(context)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.login_dialog_title)
			.setView(textEntryView)
			.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					
					final String username = user.getText();
					EditText passwordText = (EditText) textEntryView.findViewById(R.id.password_edit);
					String password = passwordText.getText().toString().trim();
					
					if(password.length() == 0) {
						ErrorDlg.show(context,
								context.getString(R.string.login_dialog_title), 
									context.getString(R.string.message_please_enter_passwd));
						return;
					}
		
					PatrolCarApp.username = username;
					PatrolCarApp.password = password;
		
					onClickListener.onClick(dialog, whichButton);
				}
			
			}).setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
		// cancel, stop login action
		}
		})
		.create();
		
		
		loginDialog.show();
	}
			
}
